package com.example.demo.pipeline.executor;

import com.example.demo.pipeline.context.TemplateContext;
import com.example.demo.pipeline.handler.TemplateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author liyuncong
 * @version 1.0
 * @file TemplateExecutor
 * @brief TemplateExecutor
 * @details TemplateExecutor
 * @date 2022-11-02
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-11-02                      liyuncong          Created
 */
@Component
public class TemplateExecutor {

    Logger log = LoggerFactory.getLogger(TemplateExecutor.class);

    @Autowired
    private Map<Class<? extends TemplateContext>,
        List<? extends TemplateHandler<? extends TemplateContext>>> templatePipelineRouteMap;

    public void executor(TemplateContext context) {
        if (null == context) {
            log.error("params not allow empty");
            throw new RuntimeException();
        }
        Class<? extends TemplateContext> dataType = context.getClass();
        Boolean flag;

        List<? extends TemplateHandler<? extends TemplateContext>> pipelineList =
            templatePipelineRouteMap.get(dataType);

        if (!CollectionUtils.isEmpty(pipelineList)) {
            for (TemplateHandler<? extends TemplateContext> handler : pipelineList) {
                flag = handler.handler(context);
                if (!flag) {
                    break;
                }
            }
        }
    }
}
